//
//  SWNetworkingManager.swift
//  SwiftTest
//
//  Created by 李江湖 on 2018/7/18.
//  Copyright © 2018年 李江湖. All rights reserved.
//

import AFNetworking


enum RequestType: String {
    case GET = "GET"
    case POST = "POST"
}
class SWNetworkingManager: AFHTTPSessionManager {

    static let shareInstance : SWNetworkingManager = {
        
        let baseUrl = NSURL(string: "xxxxxx")!
        let manager = SWNetworkingManager.init(baseURL: baseUrl as URL, sessionConfiguration: URLSessionConfiguration.default)
        manager.responseSerializer.acceptableContentTypes?.insert("text/json")
        manager.responseSerializer.acceptableContentTypes?.insert("text/plain")
        manager.responseSerializer.acceptableContentTypes?.insert("text/html")
        manager.responseSerializer.acceptableContentTypes?.insert("text/JavaScript")
        return manager
    }()
    
    /**
     get请求
     
     - parameter urlString:  请求的url
     - parameter parameters: 请求的参数
     - parameter success:    请求成功回调
     - parameter failure:    请求失败回调
     */
    class func get(urlString:String,parameters:AnyObject?,success:((_ responseObject:AnyObject?) -> Void)?,failure:((_ error:NSError) -> Void)?) -> Void {
        
        SWNetworkingManager.shareInstance.get(urlString, parameters: parameters, progress: { (progress) in }, success: { (task, responseObject) in
            
            //如果responseObject不为空时
            if responseObject != nil {
                success!(responseObject as AnyObject?)
            }
            
        }, failure: { (task, error) in
            
            failure!(error as NSError)
            
        })
    }
    
    
    /**
     post请求
     
     - parameter urlString:  请求的url
     - parameter parameters: 请求的参数
     - parameter success:    请求成功回调
     - parameter failure:    请求失败回调
     */
    class func post(urlString:String,parameters:AnyObject?,success:((_ responseObject:AnyObject?) -> Void)?,failure:((_ error:NSError) -> Void)?) -> Void {
        
        SWNetworkingManager.shareInstance.post(urlString, parameters: parameters, progress: { (progress) in
            
        }, success: { (task, responseObject) in
            
            //如果responseObject不为空时
            if responseObject != nil {
                
                success!(responseObject as AnyObject?)
            }
            
        }) { (task, error) in
            
            failure!(error as NSError)
        }
        
        
    }
}
// MARK:- 封装请求方法
extension SWNetworkingManager {
    // 网络请求公用方法
    func request(methodType: RequestType, urlString: String, parameters: [String: Any]?, finished: @escaping (_ result: Any?) -> (),failure: @escaping (_ error: Error?) -> ()) {
        
        let success = { (task: URLSessionDataTask, result: Any?) -> Void in
            finished(result)
        }
        
        let failure = { (task: URLSessionDataTask?, error: Error) -> Void in
            finished(error)
        }
        
        if methodType == .GET {
            get(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
        } else {
            post(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
        }
        
    }
}
